package com.chunuo.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.commonutils.R;
import com.chunuo.statistics.entity.Daily;
import com.chunuo.statistics.mapper.DailyMapper;
import com.chunuo.statistics.open.UcenterMemberOpen;
import com.chunuo.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-31
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UcenterMemberOpen ucenterMemberOpen;

    /**
     * 更新/新增  当天数据
     * @param day
     */
    @Override
    public void registerCount(String day) {
        //尝试获取当天数据，如果存在则是修改、不存在则新增
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("date_calculated",day);
        Daily daily = baseMapper.selectOne(wrapper);

        //获取注册的数量
        R r = ucenterMemberOpen.getRegisterCount(day);
        Integer registerCount = (Integer) r.getData().get("registerCount");

        //判断是否为null
        if(StringUtils.isEmpty(daily)){
            daily = new Daily();
            //统计日期
            daily.setDateCalculated(day);
            //注册人数
            daily.setRegisterNum(registerCount);
            //------------实际种应该实际数量------------
            //观看数量   视频被点击一次，需要记录一次
            daily.setVideoViewNum(RandomUtils.nextInt(100,200));
            //登录数量  当天的redis中有几个token，就有几次登录
            daily.setLoginNum(RandomUtils.nextInt(100,200));
            //课程添加数量   发布一次课程，做一次记录了
            daily.setCourseNum(RandomUtils.nextInt(100,200));
            System.out.println("insert === "+daily);
            baseMapper.insert(daily);
        }else{
            //注册人数
            daily.setRegisterNum(registerCount);
            System.out.println("update === "+daily);
            baseMapper.updateById(daily);
        }

    }

    /**
     * 显示指定天数区间的数据
     * @param type
     * @param before
     * @param after
     * @return
     */
    @Override
    public Map<String, Object> getEcharts(String type, String before, String after) {

        QueryWrapper<Daily> wrapper = new QueryWrapper();
//        wrapper.between("date_calculated",before,after);
        //大于before时间段
        wrapper.ge("date_calculated",before);
        //小于before时间段
        wrapper.le("date_calculated",after);
        //查询的字段
        wrapper.select("date_calculated",type);
        List<Daily> dailies = baseMapper.selectList(wrapper);

        //需要返回的格式  数组
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        // 数据获取的类型有注册人数、登录人数、视频观看数量和课程添加数量
        for(Daily daily : dailies){
            date_calculatedList.add(daily.getDateCalculated());
            switch (type){
                case "register_num" :
                    countList.add(daily.getRegisterNum());
                    break;
                case "login_num" :
                    countList.add(daily.getLoginNum());
                    break;
                case "video_view_num" :
                    countList.add(daily.getVideoViewNum());
                    break;
                case "course_num" :
                    countList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("countList",countList);

        return map;
    }
}
